package com.example.shopp_2301.search

import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.example.base.mvvm.BaseActivity
import com.example.shopp_2301.R
import com.example.shopp_2301.adapter.GoodsAdapter
import com.example.shopp_2301.databinding.ActivitySearchBinding
import com.example.shopp_2301.room.AppDbUtils
import com.example.shopp_2301.room.HistoryEntity
import com.google.android.material.chip.Chip

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        goodsAdapter = GoodsAdapter()
        binding.rvGoods.adapter = goodsAdapter
        binding.rvGoods.layoutManager = GridLayoutManager(this,2)
        //执行搜索
        binding.sv.setOnQueryTextListener(object :SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击回车键触发
                if(query!=null){
                    viewModel.search(query,10,1)
                    //保存搜索记录
                    AppDbUtils.getDb().getHisDao().insert(HistoryEntity(query,SPUtils.getInstance().getString("phone")))
                    //实时更新历史搜索记录展示
                    queryHis()
                }
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文字输入完成后，自动触发
             /*   if(newText!=null){
                    viewModel.search(newText,10,1)
                }*/
                return false
            }
        })
        //展示首次打开时的历史记录
        queryHis()
    }

    private fun queryHis() {
        //查询保存的历史记录
        val entities = AppDbUtils.getDb().getHisDao().query(SPUtils.getInstance().getString("phone"))
        //展示历史记录
        if(entities.size>0){
            //先清空之前所有的布局view
            binding.cgHis.removeAllViews()
            entities.forEach { it->
                val keyword = it.name
                val chip = LayoutInflater.from(this).inflate(R.layout.item_chip, null) as Chip
                chip.findViewById<TextView>(R.id.chipname).text = it.name

                binding.cgHis.addView(chip)

                //点击x,实现删除后台数据
                chip.setOnCloseIconClickListener { it1->
                    //删除数据库数据
                    AppDbUtils.getDb().getHisDao().delete(it)
                    //删除页面数据
                    binding.cgHis.removeView(it1)
                }

                //点击item，实现查询
                chip.setOnClickListener { it2->
                    binding.sv.setQuery(it.name,true)
                }
            }

        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            if(it.code==200){
                goodsAdapter.data.clear()
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<SearchViewModel> {
        return SearchViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.activity_search
    }

}